package com.hzgj.bcl.soa.stats;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;

/**
 * Created by noname on 15/12/26.
 */
public class LoggerStats implements Stats {
    private static final String LOGGER_NAME = "com.hzgj.bcl.soa.log.$stats";
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private Logger logger = LoggerFactory.getLogger(LOGGER_NAME);

    public LoggerStats() {
    }

    @Override
    public boolean enabled() {
        return logger != null;
    }

    @Override
    public void report(StatsData data) {
        if (logger != null) {
            logger.info("{} {} {} {} {} {} {} {} {} {} {}", DATE_FORMAT.format(data.time), data.messageId, data.contextId, data.parentId,
                    data.srcHost, data.dstHost, data.dstPort, data.service, data.method, data.success, data.span);
        }
    }

    @Override
    public String getName() {
        return "log";
    }

    @Override
    public void start() {
    }

    @Override
    public void stop() {
        logger = null;
    }
}
